from rest_framework.serializers import ModelSerializer, DateTimeField, SerializerMethodField
from .models import *


class UserSer(ModelSerializer):
    level = SerializerMethodField()

    def get_level(self, obj):
        return obj.sector.level

    class Meta:
        model = UserModel
        fields = '__all__'


class Sector2Ser(ModelSerializer):
    class Meta:
        model = SectorModel
        fields = '__all__'


class SectorSer(ModelSerializer):
    sector2 = SerializerMethodField()

    def get_sector2(self, obj):
        set = SectorModel.objects.filter(sector_id=obj.id)
        return Sector2Ser(set, many=True).data

    class Meta:
        model = SectorModel
        fields = '__all__'


class RecordSer(ModelSerializer):
    name = SerializerMethodField()
    start = DateTimeField('%Y-%m-%d %H:%M:%S')
    end = DateTimeField('%Y-%m-%d %H:%M:%S')

    def get_name(self, obj):
        return obj.user.name

    class Meta:
        model = RecordModel
        fields = '__all__'
